package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.dto.LoginDto;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author lj
 * @since 2022-06-27
 */
@Slf4j
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    /**
     * 保存自媒体用户
     * @param wmUser
     * @return
     */
    @Override
    public ResponseResult<WmUser> saveWmUser(WmUser wmUser) {
        log.info("[自媒体微服务======>自媒体用户信息表实现类，保存自媒体用户接收参数：{}]",wmUser.toString());
        // 判断用户是否存在,根据 ap_user_id 进行判断
        LambdaQueryWrapper<WmUser> queryWrapperWmUser = new LambdaQueryWrapper<>();
        queryWrapperWmUser.eq(WmUser::getApUserId, wmUser.getApUserId());
        WmUser one = this.getOne(queryWrapperWmUser);
        if(one==null){
            wmUser.setCreatedTime(new Date());
            this.save(wmUser);
        }
        return ResponseResult.okResult(wmUser);
    }

    /**
     * 自媒体用户登录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        log.info("[自媒体微服务======>自媒体用户登录实现类，接收参数：{}]",dto.toString());

        // 判断参数是否为空，用户名，密码是否为空
        if(dto==null|| StringUtils.isBlank(dto.getName())||StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 条件构造器查询，首先用户名判断
        LambdaQueryWrapper<WmUser> queryWrapperAdUser = Wrappers.lambdaQuery();
        queryWrapperAdUser.eq(WmUser::getName,dto.getName());
        WmUser wmUser = this.getOne(queryWrapperAdUser);
        // 为空判断
        if (wmUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        // 密码判断
        // md5+加盐，提高安全系数
        String password = DigestUtils.md5Hex(dto.getPassword() + wmUser.getSalt());
        if(!StringUtils.equals(password,wmUser.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        // 返回结果集构造
        Map resultMap = new HashMap();
        // user去除敏感信息，设为空
        wmUser.setSalt("");
        wmUser.setPassword("");
        wmUser.setPhone("");
        resultMap.put("user",wmUser);
        // token设置，Jwt技术
        Map claimMaps = new HashMap();
        claimMaps.put("userId",wmUser.getId());
        claimMaps.put("name",wmUser.getName());
        String token = AppJwtUtil.getToken(claimMaps);
        resultMap.put("token",token);

        return ResponseResult.okResult(resultMap);
    }

}
